/**
 * Created by jade on 16/5/25.
 */
/**
 * Created by jade on 2016/4/20.
 */
define(function (require, exports, module) {


    var Select = require("select");

    /**
     *用法
     *
     * new Sort({
     *
     *    trigger:selector
     *    model:[
     *      {value:"1",text:"选择",asc:true,desc:false,selected:true}
     *    ]
     * }).render().on("sort",function(target){
     *     //排序过后的回调
     * })
     * */
    var Sort = Select.extend({
        attrs: {
            template: require("./tpl/sort.html")
        },
        events: {
            "click [data-role=item] .icon-sort": function (event) {
                var target = $(event.currentTarget);
                var item = target.parent();
                if (!target.hasClass("ui-select-item-sort-active") && !item.data('disabled')) {
                    this._refreshRender(target, item)
                    this.trigger('sort', target);
                }
                event.preventDefault();
            },
            "click [data-role=item]": function () {

            }
        },

        select: function (selector, childSelector) {
            var item = this.options.filter(selector);
            var target = item.find(childSelector);

            //防止url篡改
            if (item.length > 0) {
                this._refreshRender(target, item);
            }
            return this;
        },

        getSelected: function (isobject) {
            var selectedIndex = this.get("selectedIndex");

            if (!isobject) {
                return selectedIndex;
            }

            var selectItem = this.options.eq(selectedIndex);
            var selectedObject = {};
            selectedObject["sortkey"] = selectItem.data("value");
            selectedObject["sortval"] = selectItem.find(".ui-select-item-sort-active").data("sort");
            return selectedObject;
        },

        resetSortType: function (selectIndex, oldSelectIndex, sortType, oldsortType) {
            var model = this.get('model');
            if (oldSelectIndex >= 0) {
                model.options[oldSelectIndex].selected = false;
                model.options[oldSelectIndex][oldsortType] = false;
            }
            if (selectIndex >= 0) {
                model.options[selectIndex].selected = true;
                model.options[selectIndex][sortType] = true;
            }
        },
        _refreshRender: function (target, item) {
            var previous = this.element.find(".ui-select-item-sort-active");
            var oldsortType = previous.data("sort");
            var selectIndex = this.options.index(item);
            var oldSelectIndex = this.get('selectedIndex');

            var sortType = target.data("sort") || oldsortType;

            this.resetSortType(selectIndex, oldSelectIndex, sortType, oldsortType);

            //重新渲染
            this.syncModel(this.get("model").options);

            this.hide();
        }


    })

    module.exports = Sort;

})/**
 * Created by Admin on 2017/12/26.
 */
